package cn.shyouchi.wuguanjia.android.ui.event.arrange;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.SelPersonRankEvent;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class AddArrangeActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_arrange)
    RecyclerView mRvArrange;

    String date;
    int id;
    int count;
    String name;
    List<PersonRankAdapter> personRankAdapters = new ArrayList<>();

    List<PersonRankAdapter> personRanks = new ArrayList<>();
    List<PersonRankAdapter.Person> selPersons = new ArrayList<>();
    SelRankAdapter selRankAdapter;


    @Override
    public int initContentView() {
        return R.layout.activity_add_arrange;
    }

    @Override
    public void initInjector() {
        date = getIntent().getStringExtra("date");
        id = getIntent().getIntExtra("id", 0);
        count = getIntent().getIntExtra("count",0);
        name = getIntent().getStringExtra("name");

        personRankAdapters = getIntent().getParcelableArrayListExtra("persons");
        selPersons = getIntent().getParcelableArrayListExtra("selPersons");

        if (personRankAdapters != null) {
            for (PersonRankAdapter rankAdapter : personRankAdapters) {
                if (!isSel(rankAdapter.getPerson())) {
                    for (PersonRankAdapter.Rank rank : rankAdapter.getRanks()) {
                        if (rank.getRank_id() == id) {
                            personRanks.add(rankAdapter);
                        }
                    }
                }

            }
        }
    }

    private boolean isSel(PersonRankAdapter.Person person) {
        if (selPersons != null) {
            for (PersonRankAdapter.Person p : selPersons) {
                if (p.getUser_id() == person.getUser_id()) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }
    }


    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(String.format("%1s%2s(%s/%s)", date, name, selPersons == null ? 0 : selPersons.size(), count));
        setSupportActionBar(mTopBar.getToolbar());

        selRankAdapter = new SelRankAdapter(personRanks, id,count);
        mRvArrange.setLayoutManager(new LinearLayoutManager(mContext));
        mRvArrange.setAdapter(selRankAdapter);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsAction(2);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {
            generateEntity();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void generateEntity() {
//        List<PersonRankAdapter.Person> persons = new ArrayList<>();
//        for (PersonRankAdapter adapter : selRankAdapter.getData()) {
//            if (adapter.isChecked()) {
//                persons.add(adapter.getPerson());
//            }
//        }
        if (!selRankAdapter.getSelPersons().isEmpty()) {
            EventBus.getDefault().post(new SelPersonRankEvent(id, selRankAdapter.getSelPersons()));
        }
        finish();
    }

}
